package com.ruanyun.modular.ry.utils;


import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CommonUtil {

    /**
     * 根据生日获取年龄
     * @param birthDay
     * @return
     * @throws Exception
     */
    public static int getAge(Date birthDay) throws Exception {
        //获取当前系统时间
        Calendar cal = Calendar.getInstance();
        //如果出生日期大于当前时间，则抛出异常
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        //取出系统当前时间的年、月、日部分
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        //将日期设置为出生日期
        cal.setTime(birthDay);
        //取出出生日期的年、月、日部分
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        //当前年份与出生年份相减，初步计算年龄
        int age = yearNow - yearBirth;
        //当前月份与出生日期的月份相比，如果月份小于出生月份，则年龄上减1，表示不满多少周岁
        if (monthNow <= monthBirth) {
            //如果月份相等，在比较日期，如果当前日，小于出生日，也减1，表示不满多少周岁
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        System.out.println("age:"+age);
        return age;
    }

    /**
     * 功能描述:获取两个日期之间相差多少小时(firstDate要大于secondDate)
     *
     * @author L H T  2014-5-21 下午03:25:30
     *
     * @param firstDate
     * @param secondDate
     * @return
     */
    public static String getBetweenHour(Date firstDate, Date secondDate){
        if (EmptyUtils.isEmpty(firstDate) || EmptyUtils.isEmpty(secondDate)) {
            return "";
        }
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String hour="";
        try {
            //获得两个时间的毫秒时间差异
            long betweenNs = sd.parse(sd.format(firstDate)).getTime() - sd.parse(sd.format(secondDate)).getTime();
            hour=formatTime(betweenNs);
//            hour = betweenNs%(1000*24*60*60)/(1000*60*60);//计算差多少小时

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hour;
    }

    /**
     * 功能描述:两个时间是否相差24小时
     *
     * @author L H T  2014-5-21 下午03:25:30
     *
     * @param firstDate
     * @param secondDate
     * @return
     */
    public static Boolean ifBetweenHours(Date firstDate, Date secondDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");

        long cha = firstDate.getTime() - secondDate.getTime();

        double result = cha * 1.0 / (1000 * 60 * 60);
         if(result<=24){
           return false; //说明小于24小时
           }else{
       return true;
}
    }


    /**
     * 功能描述:两个时间是否相差5分钟
     *
     * @author L H T  2014-5-21 下午03:25:30
     *
     * @param firstDate
     * @param secondDate
     * @return
     */
    public static Boolean ifBetweenHour(Date firstDate, Date secondDate){
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Boolean hour = false;
        try {
            //获得两个时间的毫秒时间差异
            long betweenNs = sd.parse(sd.format(firstDate)).getTime() - sd.parse(sd.format(secondDate)).getTime();
            hour =(betweenNs<=5*60*1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hour;
    }

    /*
    * 毫秒转化时分秒毫秒
    */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
//        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
//        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour >= 0) {
            if(minute > 5) {
                hour=hour+1;
            }
            sb.append(hour+"小时");
        }
//        if(minute > 0) {
//            sb.append(minute+"分");
//        }
//        if(second > 0) {
//            sb.append(second+"秒");
//        }
//        if(milliSecond > 0) {
//            sb.append(milliSecond+"毫秒");
//        }
        return sb.toString();
    }

    public File getFileByMultipartFile(MultipartFile file){
        File f = null;
        try {
            if(file.equals("")||file.getSize()<=0){
                file = null;
            }else{
                InputStream ins = file.getInputStream();
                f=new File(file.getOriginalFilename());
                FileUtil.inputStreamToFile(ins, f);
            }
        }catch (IOException e){

        }
        return f;
    }

    /**
     * 前/后?天
     *
     * @param d
     * @param day
     * @return
     */
    public static Date rollDay(Date d, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 前/后?月
     *
     * @param d
     * @param mon
     * @return
     */
    public static Date rollMon(Date d, int mon) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.MONTH, mon);
        return cal.getTime();
    }

    public static String rep(String str){
        str = str.replace("& lt;", "<");
        str = str.replace("& gt;", ">");
        str = str.replace("&nbsp;", " ");
        return str;
    }
}
